package com.gk.courses.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;

@Entity
public class Course implements Serializable {
	private static final long serialVersionUID = 434806557397763579L;
	
	private Integer courseId;
	private String name;
	private String description;
	private Date startDate;
	private Date endDate;
	private List<Student> students;
	private List<Course> prerequisites;

	public Course() {
		prerequisites = new ArrayList<Course>();
		students = new ArrayList<Student>();
	}

	@Id 
	@GeneratedValue
	public Integer getCourseId() {
		return courseId;
	}

	public void setCourseId(Integer id) {
		this.courseId = id;
	}

	@Column(nullable=false, length=25)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	@ManyToMany
	@JoinTable(
       name="join_prereq",
       joinColumns={@JoinColumn(name="courseId", referencedColumnName="courseId")},
       inverseJoinColumns= {@JoinColumn(name="prereqId", referencedColumnName="courseId")}) 
	public List<Course> getPrerequisites() {
		return prerequisites;
	}

	public void setPrerequisites(List<Course> prerequisites) {
		this.prerequisites = prerequisites;
	}
	
	@ManyToMany
 	@JoinTable(name="transcript",
		joinColumns={@JoinColumn(name="courseId")},
		inverseJoinColumns={@JoinColumn(name="id")})
	public List<Student> getStudents() {
		return students;
	}

	public void setStudents(List<Student> students) {
		this.students = students;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((courseId == null) ? 0 : courseId.hashCode());
		result = prime * result
				+ ((description == null) ? 0 : description.hashCode());
		result = prime * result + ((endDate == null) ? 0 : endDate.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result
				+ ((startDate == null) ? 0 : startDate.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Course other = (Course) obj;
		if (courseId == null) {
			if (other.courseId != null)
				return false;
		} else if (!courseId.equals(other.courseId))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Course [courseId=" + courseId + ", name=" + name
				+ ", description=" + description + ", startDate=" + startDate
				+ ", endDate=" + endDate + "]";
	}

}
